<?php
function positionSort($a, $b) {
    $rc = 0;
    if ($a->isStar()) {
        if (!$b->isStar()) {
            return 1;
        }
    } else
        if ($b->isStar()) {
            return -1;
        }
    if ($a->isJourneyman()) {
        if (!$b->isJourneyman()) {
            return -1;
        }
    } else
        if ($b->isJourneyman()) {
            return 1;
        }

    $rc = $a->cost - $b->cost;
    if ($rc == 0) {
        $rc = $b->qty - $a->qty;
    }
    if ($rc == 0) {
        $rc = strcmp($a->position, $b->position);
    }
    return $rc;

}

class Position {
    var $id;
    var $position;
    var $ma;
    var $st;
    var $ag;
    var $av;
    var $skills;
    var $singleSkillGroups;
    var $doubleSkillGroups;
    var $skillsStr;
    var $skillGroupsStr;
    var $qty;
    var $cost;
    var $availableSkills;

    function Position() {
    }

    function init($row) {
        $i = 0;
        $this->id = $row[$i++];
        $this->position = $row[$i++];
        $this->ma = $row[$i++];
        $this->st = $row[$i++];
        $this->ag = $row[$i++];
        $this->av = $row[$i++];
        $this->qty = $row[$i++];
        $this->cost = $row[$i++];
        $this->loadSkillsAndGroups();
        return $this;
    }

    function isStar() {
        return count($this->singleSkillGroups) == 0;
    }

    function isJourneyman() {
        return !strncasecmp($this->position, "journey", 6);
    }

    function loadSkillsAndGroups() {
        global $skills;
        $this->skills = array ();
        $this->singleSkillGroups = array ();
        $this->doubleSkillGroups = array ();
        $rs = executeQuery("select s.id from positions_skills ps, skills s where ps.fk_position_id=$this->id and ps.fk_skill_id=s.id order by s.name asc");
        while ($row = mysql_fetch_row($rs)) {
            $skill = $skills[$row[0]];
            $this->skills[$skill->id] = $skill;
            if (0 == strcmp("Loner", $skill->name)) {
                if (strlen($this->skillsStr) > 0) {
                    $this->skillsStr = ", " . $this->skillsStr;
                }
                $this->skillsStr = $skill->name . $this->skillsStr;
            } else {
                if (strlen($this->skillsStr) > 0) {
                    $this->skillsStr = $this->skillsStr . ", ";
                }
                $this->skillsStr = $this->skillsStr . $skill->name;
            }
        }

        $rs = executeQuery("select skill_group from position_skill_groups where fk_position_id=$this->id and double_access=0 order by skill_group");
        while ($row = mysql_fetch_row($rs)) {
            array_push($this->singleSkillGroups, $row[0]);
            $this->skillGroupsStr = $this->skillGroupsStr . $row[0];
        }

        $this->skillGroupsStr = $this->skillGroupsStr . "-";
        $rs = executeQuery("select skill_group from position_skill_groups where fk_position_id=$this->id and double_access=1 order by skill_group");
        while ($row = mysql_fetch_row($rs)) {
            array_push($this->doubleSkillGroups, $row[0]);
            $this->skillGroupsStr = $this->skillGroupsStr . $row[0];
        }
        if ($this->isStar()) {
            $this->skillGroupsStr = "Star";
        }
    }

    function getAvailableSkills() {
        if (!isset ($this->availableSkills)) {
            $this->availableSkills = $this->getSkillCategory($this->singleSkillGroups);
            $this->availableSkills = array_merge($this->availableSkills, $this->getSkillCategory($this->doubleSkillGroups));
            $this->availableSkills = array_merge($this->availableSkills, $this->getSkillGroup(""));
            $this->availableSkills = array_key_diff($this->availableSkills, $this->skills);
        }

        return $this->availableSkills;
    }

    function getSkillCategory($category) {
        $skills = array ();
        foreach ($category as $g) {
            $skills = array_merge($skills, $this->getSkillGroup($g));
        }
        uasort($skills, "skillAlphaSort");
        return $skills;
    }

    function getSkillGroup($group) {
        global $skills;
        $rs = array ();
        foreach ($skills as $skill) {
            if (0 == strcmp($skill->group, $group)) {
                $rs[$skill->id] = $skill;
            }
        }
        return $rs;
    }

}
?>